package www.dianbaozu.com.ui.server

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentGroupingSortingBinding
import www.wen.com.common.lib.base.fragment.BaseVMFragment
import www.wen.com.common.lib.data.entities.GroupingEntity
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import java.util.*


/**
 * @author WEN
 * @Description:所属分组
 * @date 2020//25 17:48
 */
open class GroupingSortingFragment : BaseVMFragment<FragmentGroupingSortingBinding>(),
    View.OnClickListener,
    OnItemClickListener {

    private lateinit var mGroupingList: ArrayList<GroupingEntity>

    private lateinit var mGroupingSortingAdapter: GroupingSortingAdapter

    var mCurrentPosition:Int = -1

    override fun isEnableLoadingHelper() = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(
        requireActivity(),
        tilte = getString(R.string.subordinate_group),
        action = ::titleAction
    )

    override fun getLayoutId(): Int = R.layout.fragment_grouping_sorting

    override fun lazyInitView() {
        mGroupingList = requireArguments().getParcelableArrayList(BUNDLE_DATA)!!
        dataBinding.rvChannel.run {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = GroupingSortingAdapter(mGroupingList.toMutableList()).apply {
                setOnItemClickListener(this@GroupingSortingFragment)
                mGroupingSortingAdapter = this
            }
        }
        mCurrentPosition = mGroupingList.indexOfFirst{ it.isSelected }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        if(position!= mCurrentPosition){
            val clickPosition = adapter.data[position] as GroupingEntity
            val currentPosition = adapter.data[mCurrentPosition] as GroupingEntity
            clickPosition.isSelected = true
            currentPosition.isSelected = false
            adapter.notifyItemChanged(mCurrentPosition)
            adapter.notifyItemChanged(position)
            mCurrentPosition = position
            //通知创建新频道页面刷新选择分组
            postObject(KeyHelper.CHANNEL_CREATE_FRAGMENT_UI_KEY,clickPosition)
            actionLeft()
        }
    }

    internal class GroupingSortingAdapter(groups:MutableList<GroupingEntity>): BaseQuickAdapter<GroupingEntity, BaseViewHolder>(R.layout.item_grouping_choose,groups){

        override fun convert(holder: BaseViewHolder, item: GroupingEntity) {
            holder.setVisible(R.id.imageView4,item.isSelected)
            holder.setText(R.id.tvCreateGroup,item.groupName)
        }

    }


}



